<template>
  <div>
    <el-input placeholder="输入关键字进行过滤" v-model="filterText"></el-input>
    <el-tree
      :data="menus"
      :props="defaultProps"
      node-key="catId"
      ref="menuTree"
      @node-click="nodeclick"
      :filter-node-method="filterNode"
      :highlight-current = "true"
    >
    </el-tree>

  </div>
</template>

<script>
/**
 * 父子组件传值
 * this.$emit("事件名",携带的数据...)
 */
import {
  getCategoryListTree,
} from '../../../api/product/category'

export default {
  data() {
    return {
      filterText: "",
      menus: [],
      expandedKey: [],
      defaultProps: {
        children: "children",
        label: "name",
      },
    };
  },
  watch:{
    filterText(value){
      this.$refs.menuTree.filter(value)
    }
  },
  methods: {
    //树节点过滤
    filterNode(value, data) {
      if (!value) return true;
      return data.name.indexOf(value) !== -1;
    },
    //获取所有数据
    getMenus() {
      getCategoryListTree().then((response) => {
        this.menus = response.data.data;
      });
    },
    nodeclick(data, node, component) {
      console.log("子组件category的节点被点击", data, node, component);
      //向父组件发送事件；
      this.$emit("tree-node-click", data, node, component);
    }
  },
  created() {
    this.getMenus();
  },
};
</script>

<style>
</style>